package org.yyf.resource.demo.socket;

import java.io.IOException;
import java.net.Socket;
import org.apache.commons.lang3.StringUtils;

/**
 * 模拟服务端处理线程
 * @author YYF
 *
 */
public class SocketThead implements Runnable{
	Socket socket;
	
	public SocketThead(Socket socket) {
		super();
		this.socket = socket;
	}



	@Override
	public void run(){
		
		try {
			byte[] bytes = new byte[1024];
			int len;
			StringBuffer buffer = new StringBuffer();
			while ((len = socket.getInputStream().read(bytes)) != -1) {
				// 注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
				buffer.append(new String(bytes, 0, len, "UTF-8"));
			}
			
			System.out.println("接收命令：" + buffer);
			
			if (StringUtils.contains(buffer, "QUERY C280 USER")) {
				socket.getOutputStream().write(
						"ACK:QUERY C280 USER:  RETN=0, DESC=\"查询用户成功。\";, TOTAL=1, FINISHED=1, START=0, ROWNUM=1, ATTR=LOGINNAME & ACCOUNTNUMBER & USERID & USERNAME & IDNUMBER & CONTACTMAN & PAYMODE & COMPANY & USERADDRESS & BANK & BANKACCOUNT & PHONE & PAYPHONE & ACCOUNTTYPE & ACCESSRIGHT & ZIPCODE & MOBILE & EMAIL & BP & DESCRIPTION & SERVICELEVEL & MAXUSERNUMBER & IPADDRESS & IPMASK & STATUS & TEMPLATEID & TEMPLATENAME & IPPOOLID & IPPOOLTYPE & IPPOOLNAME & LIMITFLAG & LIMITPORT & ACCESSPOLICYID & AREAID & PROVIDERID & ACCPROFID & ACCPROFNAME & REGISTERTIME & UNREGISTERTIME & SUBSTATE & INARREAR & ACCOUNTLEFT & NICKNAME & DISABLEPORTALURL & CUI & CALLINGIDCHECKFLAG & CALLINGSTATIONID & BATCH & FIRSTUSEDATE & QTYTYPE & QUANTITY & ROAMINGLEVEL & REGION & OCSCONNID & PRESENTMONEYLEFT & BINDFLAG & BINDTYPE & NASIPADDRESS & NASIDENTIFY & NASPORTID & VLANID & LASTUSEBEGINTIME & LASTUSEENDTIME & IWLANAUTHENABLED & IMSI & MSISDN & AREANAME & ACCESSPOLICYNAME  & AUTHTYPE & FRAINTERFACEID & FRAIPV6PREFIX & DELIPV6PRE & FRAIPV6ADDR & SWITCH1 & SWITCH2 & IPADDRESSSTR & IPMASKSTR & CVLANID & QUANTITYVOLUM & STOPREASON  , RESULT=\"18307218756@jtkd|18307218756@jtkd||5170948328|5170948328||1|||||5170948328||1|3||5170948328|null|||1|1|-1|-1|1|71|100M-NEW01|-1|||1|11432-trunk1/1/4:2056.269210.94.40.49/0/0/18/0/3/00000000434D4443B2AF1173GP^B|-1|-1|200000|-1||20190201092253|20370101000000|1|0|999999999||0||0||0|20190201180959||00000000000000|0||11|0|1|9|223.75.75.15|HBXYA-MC-CMNET-BAS15-GC-ME60-|trunk1/1/4:2056.269210.94.40.49/0/0/18/0/3/00000000434D4443B2AF1173GP^B|-1||20190828175043|0|******||||1|||||0|0|255.255.255.255|255.255.255.255|-1|0|0|\";"
								.getBytes("UTF-8"));
			}
			if (StringUtils.contains(buffer, "QUERY C280 USERSESS")) {
				socket.getOutputStream().write(
						"ACK:QUERY C280 USERSESS:  RETN=0, DESC=\"查询会话记录成功。\";, TOTAL=1, FINISHED=1, START=0, ROWNUM=1, ATTR=USERNAME & USERIP & ISNIP & NASPORTID & SERSTARTTIME & ACCTSESSIONID & INPUTVOLUMN & DOMAIN & NODENAME & OUTPUTVOLUMN & NASPORTTYPE & NASPORT & MAC & NATIPADDRESS & HWNATSTARTPORT & HWNATENDPORT  , RESULT=\"15907290320|1681920920|2026507501|trunk3/0/9:1575.3987172.24.252.4/1/1/1/0/9/000000000000CMDCD50D54BDGP|20190828175043|HBXIG-M03230157503987f1f84f188567|2344879|jtkd|XG-HUAWEI-BRAS-120.202.12.237|53544121|15||AC-5A-EE-D8-25-89|1972896911|17408|21503|\";"
								.getBytes("UTF-8"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(socket!=null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}
